package com.minelx.sqlstructures.statements.parse.select;

import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;
import com.minelx.sequence.pattern.Pattern;
import com.minelx.sequence.pattern.variable.VariableManager;
import com.minelx.sqlstructures.statements.clause.Clauses;
import com.minelx.sqlstructures.statements.clause.Sql;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.lang.Integer.parseInt;
import static java.util.stream.Collectors.joining;

public class Parsers {
	public static ReturnMatcher<Sql, StringScanner> limit() {
		return new VariableMappingMatcher<>(
				Pattern.create(" *LIMIT *${count:[DIGIT]+}").matcher(),
				variableManager -> {
					// FIXME 2021/8/11  wait for me!!!
					ArrayList<?> list = (ArrayList<?>) variableManager.get("count");
					ArrayList<Character> inner = (ArrayList<Character>) list.get(0);
					return Clauses.limit(parseInt(inner.stream().map(String::valueOf).collect(joining())));
				}
		);
	}
}
